<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>部门</title>
<head>
<jsp:include page="../inc/meta.jsp"></jsp:include>
<jsp:include page="../inc/easyui.jsp"></jsp:include>
</head>

<body>
	<table id="department">
	</table>
	<div id="CreateDepartWin" collapsible="false" closed="true"
		resizable="true" modal="true" minimizable="false" maximizable="false"
		style="display: none; width: 450px; height: 300px;
        padding: 5px; background: white;">
		<form id="frmDept" action="" method="post" class="l-form">
			<input name="Id" id="deptId" type="hidden" />
			<div class="l-group l-group-hasicon">
				<img
					src="<%=request.getContextPath()%>/Content/icons/communication.gif">
				<span>部门信息</span>
			</div>
			<ul>
				<li style="width: 100px; text-align: left;">部门名称：</li>
				<li style="width: 220px; text-align: left;"><input name="Name"
					id="deptName" class="easyui-validatebox" required="true"
					validtype="length[1,50]" style="width: 100%">
				</li>
			</ul>
			<ul>
				<li style="width: 100px; text-align: left;">部门排序：</li>
				<li style="width: 220px; text-align: left;"><input name="Sort"
					id="deptSort" class="easyui-validatebox" required="true"
					validtype="onlyNumber[5]" style="width: 100%">
				</li>
			</ul>
			<ul>
				<li style="width: 100px; text-align: left;">上级部门：</li>
				<li style="width: 220px; text-align: left;"><input
					name="_parentId" id="deptParent" class="easyui-combotree"
					style="width: 220px" />
				</li>
			</ul>
			<ul>
				<li style="width: 100px; text-align: left;">部门描述：</li>
				<li style="width: 220px; text-align: left;"><textarea
						name="Desc" id="deptDesc" style="resize: none; width: 100%"></textarea>
				</li>
			</ul>

		</form>
	</div>
</body>

<script type="text/javascript">
        $(function () {
            var departmentGrid=$('#department').treegrid({
                title: '部门',
                url: '<%=request.getContextPath()%>/department/treeGrid',
                idField: 'id',
                treeField: 'name',
                fitColumns:true,
                rownumbers: true,
                toolbar: [
                    {
                        text: '创建',
                        iconCls: 'icon-add',
                        handler: function () {
                            createDepart();
                        }
                    }, '-', {
                        text: '删除',
                        iconCls: 'icon-remove',
                        handler: function () {
                        	deleteDepart();
                        }
                    }, '-', {
                        text: '编辑',
                        iconCls: 'icon-edit',
                        handler: function () {
                        	editDepart();
                        }
                    }, '-', {
                        text: '取消选中',
                        iconCls: 'icon-undo',
                        handler: function () {
                        	departmentGrid.datagrid('unselectAll');
                        }
                    }],
                frozenColumns: [[
	                      { field: 'ck', checkbox: true }
                        , { field: 'name', title: '名称', width: 240 }
				    ]],
                columns: [[
					  { field: 'desc', title: '描述', width: 200 }
					, { field: 'createTime', title: '创建时间', width: 120 }
					, { field: 'updateTime', title: '更新时间', width: 120 }
					
				    ]]
            });

//             $("#deptParent").combotree({
//                 url: '/DepartmentTree/GetTree',
//                 onShowPanel: function () {
//                     $("#deptParent").combotree('reload');
//                 }
//             });
        });
        //提示选择信息
        function getSelectId() {
            var node = $('#department').treegrid('getSelected');
            if (!node) {
                $.messager.show({
                    title: '提示',
                    msg: '请选择数据！',
                    showType: 'show'
                });
                return null;
            } else {
                return node.Id;
            }
        }
        
        //创建部门
        function createDepart(){
        	var parentId="";
        	var node=$('#department').treegrid('getSelected');
        	if(node!=null){
        		parentId=node.id;
        	}
        	var fromUrl = '<%=request.getContextPath()%>/department/toCreateDepartment?parentId='+parentId;
			var toUrl='<%=request.getContextPath()%>/department/CreateDepartment';
             var p = top.sy.dialog({
                 title: '创建部门',
                 iconCls: 'icon-add',
                 href: fromUrl,
                 width: 500,
                 height: 400,
                 modal: true,
                 minimizable: false,
                 maximizable: false,
                 cache: false,
                 buttons: [{
                     text: '确定',
                     iconCls: 'icon-ok',
                     handler: function () {
                         var f = p.find('form');
                         f.form('submit', {
                             url: toUrl,
                             onsubmit: function () {
                                 var flag = $(this).form('validate');
                                 if (flag) {
                                	 top.showProcess(true, '温馨提示', '正在提交数据...');
                                 }
                                 return flag;
                             },
                             success: function (data) {
                            	 top.showProcess(false);
                                 var map = $.parseJSON(data);
                                 if (map.success) {
                                	 top.showMsg('提示', map.msg);
                                     reloadDataGrid();
                                     p.dialog('close');
                                 }
                                 else {
                                	 top.alertMsg('错误', map.msg+"\n"+map.obj==null?"":map.obj);
                                 }

                             },
                             onLoadError: function () {
                            	 top.showProcess(false);
                                 $.messager.alert('温馨提示', '由于网络或服务器太忙，提交失败，请重试！');
                             }
                         });

                     }
                 }, {
                     text: '取消',
                     iconCls: 'icon-cancel',
                     handler: function () {
                         p.dialog('close');
                     }
                 }]
             });

        }
        
        //编辑部门
        function editDepart() {
        	  var node=$('#department').treegrid('getSelected');
              if (node!=null) {
//                   if(node.parentId==null)
//                   {
//                      top.showMsg("提示", "该部门不能编辑！");
//                      return;
//                   }
                      //弹出编辑框
                 	 var url='<%=request.getContextPath()%>/department/toEditDepartment?deptId='+node.id;
                      var p = top.sy.dialog({
                          title: '编辑部门',
                          iconCls: 'icon-edit',
                          href: url,
                          width: 500,
                          height: 400,
                          modal: true,
                          minimizable: false,
                          maximizable: false,
                          cache: false,
                          buttons: [{
                              text: '确定',
                              iconCls: 'icon-ok',
                              handler: function () {
                                  var f = p.find('form');
                                  f.form('submit', {
                                      url: '<%=request.getContextPath()%>/department/EditDepartment',
                                      onsubmit: function () {
                                          var flag = $(this).form('validate');
                                          if (flag) {
                                        	  top.showProcess(true, '温馨提示', '正在提交数据...');
                                          }
                                          return flag;
                                      },
                                      success: function (data) {
                                    	  top.showProcess(false);
                                          var map = $.parseJSON(data);
                                          if (map.success) {
                                        	  top.showMsg('提示', map.msg);
                                              reloadDataGrid();
                                              p.dialog('close');
                                          }
                                          else {
                                        	  top.alertMsg('错误', map.msg+"\n"+map.obj==null?"":map.obj);
                                          }

                                      },
                                      onLoadError: function () {
                                    	  top.showProcess(false);
                                          $.messager.alert('温馨提示', '由于网络或服务器太忙，提交失败，请重试！');
                                      }
                                  });

                              }
                          }, {
                              text: '取消',
                              iconCls: 'icon-cancel',
                              handler: function () {
                                  p.dialog('close');
                              }
                          }]
                      });
                      }
                
              else {
                  showMsg("提示", "请选择要编辑的记录！");
              }  

        }
        
        //删除部门
        function deleteDepart(){
        	var node=$('#department').treegrid('getSelected');                
            if (node!=null) {
//              if(node.parentId==null)
//                   {
//                      top.showMsg("提示", "该部门不能删除！");
//                      return;
//                   }
            	top.$.messager.confirm('提示', '确定要删除吗？',
						function(r) {
							if (r) {
                        $.ajax({
								url : '<%=request.getContextPath()%>/department/deleteDepartment',
								data : {
									departmentId : node.id
								},
								cache : false,
								dataType : "json",
								success : function(data) {
									if (data.success) {
									$('#department').treegrid('unselectAll');
										reloadDataGrid();
										top.showMsg('提示', data.msg);
									} else {
										top.alertMsg('警告', data.msg+"\n"+data.obj==null?"":data.obj);
									}
								}
							});
							}
            	});
                    }
            else {
                showMsg("提示", "请选择要删除的记录！");
            }  
                
        }
        function getView() {
            var deptId = getSelectId();
            if (deptId) {
                var url = "<%=request.getContextPath()%>/Department/Details/" + deptId;
                //添加新页面
                top.addTab("部门详细", url, "icon-search");
            }
        }



        
        function reload() {
            var node = $('#department').treegrid('getSelected');
            if (node) {
                $('#department').treegrid('reload', node.code);
            } else {
                $('#department').treegrid('reload');
            }
        }
        
        function remove() {
            var node = $('#department').treegrid('getSelected');
            if (node) {
                $('#department').treegrid('remove', node.Id);
            }
        }
        
        function reloadDataGrid(){
        	$('#department').treegrid('clearChecked');
        	$('#department').treegrid('reload');
        }

    </script>
</html>
